package org.zht.dormpro.auth.controller;

import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zht.dormpro.auth.bean.Tree;
import org.zht.dormpro.pub.constant.Enums;
import org.zht.dormpro.pub.exception.DataBaseException;
import org.zht.dormpro.auth.model.SysPermission;
import org.zht.dormpro.auth.service.IMenuService;
import org.zht.dormpro.auth.service.IRoleService;
import org.zht.dormpro.auth.service.ISysPermissionService;
import org.zht.dormpro.pub.ResponseMap;
import org.zht.dormpro.pub.util.ShiroUtil;

import java.util.List;

@Controller
@RequestMapping("/auth/menu")
public class MenuController {
    @Autowired
    IMenuService menuService;
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/listMenu")
    @ResponseBody
    public List<SysPermission> listMenu(String menuName) {
        return sysPermissionService.listMenu(menuName);
    }

    @RequestMapping("menuTreePage")
    public String menuTreePage(){
        return "menu/menuTree";
    }

    @RequestMapping("/listMenuTree")
    @ResponseBody
    public ResponseMap getMenuTree(){
        Tree<SysPermission> menuTree = menuService.getMenuTree(false);
        return ResponseMap.success(menuTree);
    }
    //只包含菜单
    @RequestMapping("/listOnlyMenuTree")
    @ResponseBody
    public ResponseMap getOnlyMenuTree(){
        Tree<SysPermission> menuTree = menuService.getMenuTree(true);
        return ResponseMap.success(menuTree);
    }
    //根据角色信息列出tree
    @RequestMapping("/listMenuTree/{roleId}")
    @ResponseBody
    public ResponseMap getMenuTreeById(@PathVariable("roleId") Integer roleId){
        Tree<SysPermission> menuTree = menuService.getMenuTreeById(roleId);
        return ResponseMap.success(menuTree);
    }


    @RequestMapping("/addMenuPage/{parentId}")
    public String menuAddPage(Model model ,@PathVariable("parentId") Integer parentId){
        model.addAttribute("parentId",parentId);
        if(parentId==0){
            model.addAttribute("parentName","根目录");
        } else {
            SysPermission sysPermission = menuService.getSysPermission(parentId);
            model.addAttribute("parentName",sysPermission.getName());
        }
        return "menu/menu_add";
    }
    @RequestMapping("/editMenuPage/{id}")
    public String menuEditPage(Model model,@PathVariable("id") Integer id){
        //查询当前资源信息
        SysPermission sysPermission = sysPermissionService.getSysPermission(id);
        model.addAttribute("menu",sysPermission);
        model.addAttribute("parentName","根目录");
        //查询父节点的名称
        if(sysPermission.getParentId()!=0){
            SysPermission parent = sysPermissionService.getSysPermission(sysPermission.getParentId());
            model.addAttribute("parentName",parent.getName());
        }
        return "menu/menu_edit";
    }
    @ResponseBody
    @RequestMapping(value = "/editMenu",method = RequestMethod.POST)
    public ResponseMap editMenu(SysPermission newMenu){
        boolean success = sysPermissionService.editMenu(newMenu);
        if(success){
            return ResponseMap.success("菜单更新成功!");
        } else{
            return ResponseMap.error("菜单更新失败!");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/addMenu",method = RequestMethod.POST)
    public ResponseMap addMenu(SysPermission menu) {
        //设置其他信息
        menu.setAvailable(1);
        menu.setCreateTime(System.currentTimeMillis());
        menu.setModifyTime(System.currentTimeMillis());
        menu.setCreator(ShiroUtil.getAuthUser().getUserId());
        menu.setMender(ShiroUtil.getAuthUser().getUserId());
        if(StringUtils.isBlank(menu.getUrl())){
            menu.setUrl("----");
        }
        //取得父节点的parentid
        SysPermission sysPermission = sysPermissionService.getSysPermission(menu.getParentId());
        menu.setParentIds(sysPermission==null?"0":sysPermission.getParentIds()+"/"+sysPermission.getId());
        int id = 0;
        try {
            id = sysPermissionService.addMenu(menu);
            if(id>0){
                return ResponseMap.success("菜单添加成功!");
            } else {
                return ResponseMap.error("菜单添加失败!");
            }
        } catch (DataBaseException e) {
            return ResponseMap.error(e.getMessage());
        }

    }
    @RequestMapping("/deleteMenu")
    @ResponseBody
    public ResponseMap deleteMenu(Integer menuId){
        //删除操作
        //删除权限表
        boolean success = sysPermissionService.deleteMenu(menuId);
        //删除权限角色表
        if(success){
            boolean ok = roleService.deleteMenuPerRL(menuId, Enums.MRStatus.MENU);
            if(ok){
                return ResponseMap.success("菜单删除成功!");
            }
        }
        return ResponseMap.success("菜单删除失败!");
    }


    @RequestMapping("/menuPage")
    public String menu(Model model) {
        //查询菜单,暂只支持一级菜单查询
        List<SysPermission> list = sysPermissionService.getHasChildMenu();
        model.addAttribute("menus",list);
        return "menu/menu_list";
    }
    @RequestMapping("/hasChildMenu")
    @ResponseBody
    public ResponseMap hasChildMenu() {
        //查询菜单,暂只支持一级菜单查询
        List<SysPermission> list = sysPermissionService.getHasChildMenu();
        return ResponseMap.success(list);
    }
}
